package com.aier.user.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.school.entity.School;
import com.aier.school.service.ISchoolService;
import com.aier.user.entity.User;
import com.aier.user.service.IFindPeopleService;

@Scope("prototype")
@Component("userLookUpAction")
public class UserLookUpAction extends UserBaseAction{

    /**
     * 找人、找学校
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name="findPeopleService")
    private IFindPeopleService findPeopleService;
    @Resource(name="classService")
    private IClassService classService;
    @Resource(name="schoolService")
    private ISchoolService schoolService;
    
    // 错误码
    private int s;
    // 当前页数
    private int page;
    // 每页的记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总记录数
    private int count;
    
    // 学校id
    private Integer sid;
    // 班级id
    private Long cid;
    // 性别
    private String sex;
    // 昵称
    private String nickname;
    
    private List<User> userList;
    // 班级列表
    private List<Classes> classList;
    
    // 被访问者的用户id(被查看详情的用户id)
    private Long buid;
    
    // 详细资料
    private User userInfo;
    
    // 学校所在区域id
    private Integer scid;
    // 学校名称
    private String sname;
    
    private Long Uid;
    // 学校列表
    private List<School> schoolList;
    
    @Override
    public String execute(){
        // 查询个人资料
        userInfo = findPeopleService.searchSelfInfo((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        s = 1;
        return SUCCESS;
    }

    /**
     * 查询学校的所有班级
     * @return
     */
    public String searchAllClasses() {
        classList = classService.searchClassList(sid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 根据幼儿园、班级、性别、昵称查找
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchPeopleByCondition() {
        
        if (null == sid) {
            s = Constant.num_10001;
            return SUCCESS;
        }
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = findPeopleService.searchPeaople(page, page_size,sid.longValue(), cid, sex, nickname);
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            userList = (List<User>) map.get(ICommonDAO.OBJ_LIST);
                
        }
        s = 1;
        
        return SUCCESS;
    }
    
    /**
     * 查询感兴趣的人
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchInterestPeople() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        userList = findPeopleService.searchInterestPeaople(Uid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 新增访客记录
     * @return
     */
    public String insertVisitor() {
        s = findPeopleService.insertVisitor((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), buid);
        return SUCCESS;
    }
    
    /**
     * 查询最近来访
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchVisitor() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = findPeopleService.searchVisitor(page, page_size, 
                (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            count =(Integer) map.get(ICommonDAO.COUNT);
            
            userList = (List<User>) map.get(ICommonDAO.OBJ_LIST);
                
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查看较详细资料（浮出框）
     * @return
     */
    public String searchInfoDetail() {
        userInfo = findPeopleService.searchInfoDetail((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), buid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 根据城市id和学校姓名查询学校列表
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchSchoolList() {
        
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = schoolService.searchSchoolList(page, page_size, scid, sname);
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            schoolList = (List<School>) map.get(ICommonDAO.OBJ_LIST);
                
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 根据城市id查询学校列表
     * @return
     */
    public String searchSchoolByCid() {
        schoolList = schoolService.searchSchoolByCid(scid);
        s = 1;
        return SUCCESS;
    }
    
    // 新版
    /**
     * 根据帐号或昵称查找
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchPeople() {
        
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = findPeopleService.searchPeaople(page, page_size,nickname,Uid);
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            userList = (List<User>) map.get(ICommonDAO.OBJ_LIST);
                
        }
        s = 1;
        
        return SUCCESS;
    }

    public void setFindPeopleService(IFindPeopleService findPeopleService) {
        this.findPeopleService = findPeopleService;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getCount() {
        return count;
    }

    public List<User> getUserList() {
        return userList;
    }

    public int getS() {
        return s;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public List<Classes> getClassList() {
        return classList;
    }

    public void setBuid(Long buid) {
        this.buid = buid;
    }

    public User getUserInfo() {
        return userInfo;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public void setScid(Integer scid) {
        this.scid = scid;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public List<School> getSchoolList() {
        return schoolList;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getPage() {
        return page;
    }

	public void setUid(Long uid) {
		Uid = uid;
	}

}